/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.util;
/*
 * ThaiUtil.java
 * Created on 20 August 2007, 19:13
 */

public class ThaiUtil {

    public static String Unicode2ASCII(String unicode) {

        StringBuffer ascii = new StringBuffer(unicode);
        int code;

        for (int i = 0; i < unicode.length(); i++) {
            code = (int) unicode.charAt(i);
            if ((0xE01 <= code) && (code <= 0xE5B)) {
                ascii.setCharAt(i, (char) (code - 0xD60));
            }
        }

        return ascii.toString();

    }

    public static String ASCII2Unicode(String ascii) {

        StringBuffer unicode = new StringBuffer(ascii);
        int code;

        for (int i = 0; i < ascii.length(); i++) {
            code = (int) ascii.charAt(i);
            if ((0xA1 <= code) && (code <= 0xFB)) {
                unicode.setCharAt(i, (char) (code + 0xD60));
            }
        }

        return unicode.toString();

    }

    public static String checkMessageLang(String messageContent) {
        String returnValue = "E";
        String tmpCheckMessage;
        tmpCheckMessage = Unicode2ASCII(messageContent);

        for (int i = 0; i <= tmpCheckMessage.length() - 1; i++) {
            char t = tmpCheckMessage.charAt(i);
            int asciiValue = (int) t;
            if (asciiValue > 159) { // Found Thai set thaiChar flag
                returnValue = "T";
                break;
            }
        }
        return returnValue;
    }

    public static String convertText(String text) {

        /*
         * Method : convertText
         *
         */

        String messageData = "";

        try {

            for (int i = 0; i <= text.length() - 1; i++) {
                char getChar = text.charAt(i);
                int unicodeValue = (int) getChar;
                String unicodeInHex = Integer.toString(unicodeValue, 16);
                if (unicodeInHex.length() == 1) {
                    unicodeInHex = "0" + unicodeInHex;
                }

                messageData += unicodeInHex.toUpperCase();
            }

        } catch (Exception e) {
            messageData = "";
        }
        return messageData;
    }

    public static String printBytes(byte[] array) {
        String returnByte;
        returnByte = "";
        for (int k = 0; k < array.length; k++) {
            //System.out.println(name + "[" + k + "] = " + "0x" + UnicodeFormatter.byteToHex(array[k]));
            returnByte += UnicodeFormatter.byteToHex(array[k]);


        }
        return returnByte;
    }

    public static String convertThai(String text) {

        char oprcmd = '%';
        String messageData;
        messageData = "";

        // Convert ascii to unicode
        text = ASCII2Unicode(text);

        for (int i = 0; i <= text.length() - 1; i++) {
            char getChar;
            getChar = text.charAt(i);
            int unicodeValue;
            unicodeValue = (int) getChar;
            String unicodeInHex = Integer.toString(unicodeValue, 16);
            if (unicodeInHex.length() == 3) {
                unicodeInHex = oprcmd + "0E" + oprcmd + unicodeInHex.substring(1, 3);
            } else if (unicodeInHex.length() == 1) {
                unicodeInHex = oprcmd + "00" + oprcmd + "0" + unicodeInHex;
            } else {
                unicodeInHex = oprcmd + "00" + oprcmd + unicodeInHex;
            }
            messageData += unicodeInHex.toUpperCase();
        }
        return messageData;
    }
}
